﻿using System;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.News;
using ChannelVN.Cached.Base;
using ChannelVN.Cached.UrlRewrite;
using GUI.Controls.Sport;
using GUI.Extensions;
using ChannelVN.Cached;

namespace GUI.Pages.Sport
{
    public partial class ListNews : PageBase
    {
        protected override string CacheConfigName
        {
            get
            {
                return "Pages-Sport-ListNews";
            }
        }

        protected override bool IsCachedChanged(long newsId, string zoneId, long tagId)
        {
            return !string.IsNullOrEmpty(zoneId);
        }

        private const int PageSize = 10;
        private const int NumberOfPageLoadFromPositionTable = 1;
        private const int TopOtherHighlightItems = 3;
        private const int TopHighlight = 1;
        private const int TopHauTruong = 6;

        public int ZoneId = 0;


        protected void Page_Load(object sender, EventArgs e)
        {
            var objCat = RequestExtensions.Category;
            if (objCat != null)
            {
                ZoneId = objCat.Id;

                var pageIndex = RequestExtensions.PageIndex;
                var containsPageIndex = RequestExtensions.ContainsPageIndex;
                var laMucHauTruong = objCat.Id == DefinedConstants.Zone.HauTruong;
                var bo = CacheSettingExtensions.EnableCacheHtml ? NewsBO_NoCache.GetInstance() : NewsBO.GetInstance();
                var isCachedUpdating = CacheSettingExtensions.EnableCacheHtml && CacheController.IsCachedUpdating;
                #region Tin nổi bật

                {
                    if (laMucHauTruong)
                    {
                        var ctrNoiBatHauTruong = (HighlightNewsInCategory2)LoadControl("/Controls/Sport/HighlightNewsInCategory2.ascx");
                        if (ctrNoiBatHauTruong != null)
                        {
                            cphHighlightZone.Controls.Add(ctrNoiBatHauTruong);
                            ctrNoiBatHauTruong.Visible = ctrNoiBatHauTruong.DataBind(
                                bo.GetSportNewsByPosition(
                                    top: TopHauTruong,
                                    typeId: DefinedConstants.SportNews.TypeIds.SlideTrangChu,
                                    zoneId: objCat.Id,
                                    getDataFromMasterDataBase: isCachedUpdating));
                        }
                    }
                    else
                    {
                        var ctrHighLightInSubCategory = (HighlightNewsInCategory)LoadControl("/Controls/Sport/HighlightNewsInCategory.ascx");
                        if (ctrHighLightInSubCategory != null)
                        {
                            cphHighlightZone.Controls.Add(ctrHighLightInSubCategory);
                            var dtTopItem = bo.GetSportNewsByPosition(
                            top: TopHighlight,
                            typeId: DefinedConstants.SportNews.TypeIds.NoiBatChuyenMuc,
                            zoneId: objCat.Id,
                            getDataFromMasterDataBase: isCachedUpdating);

                            var dtOtherItem = bo.GetSportNewsByPosition(
                            top: TopOtherHighlightItems,
                            typeId: DefinedConstants.SportNews.TypeIds.TinTrangChuyenMuc,
                            zoneId: objCat.Id,
                            getDataFromMasterDataBase: isCachedUpdating);

                            ctrHighLightInSubCategory.Visible = ctrHighLightInSubCategory.DataBind(dtTopItem, dtOtherItem);
                        }
                    }

                }
                #endregion


                #region List

                {
                    var topExclusion = laMucHauTruong ? 0 : TopOtherHighlightItems;
                    var dt = pageIndex <= NumberOfPageLoadFromPositionTable
                                    ? bo.GetSportNewsByPosition(top: PageSize, typeId: DefinedConstants.SportNews.TypeIds.TinTrangChuyenMuc, topExclusion: topExclusion, zoneId: objCat.Id, getDataFromMasterDataBase: isCachedUpdating)
                                    : bo.GetListNews(pageIndex, PageSize, topExclusion: topExclusion, zoneId: objCat.Id);


                    ctrListNews.Visible = ctrListNews.DataBind(dt);
                    if (ctrListNews.Visible)
                    {
                        ctrPaging.LinkSite = RequestExtensions.Url;

                        ctrPaging.SumPage = bo.GetTotalPagesOfListNews(PageSize, zoneId: objCat.Id);

                        ctrPaging.Padding(pageIndex);
                    }
                }
                #endregion

                #region Đọc nhiều nhất / Lịch thi đấu
                {

                    if (objCat.Id == DefinedConstants.Zone.BongDaAnh ||
                    objCat.Id == DefinedConstants.Zone.BongDaTBN ||
                    objCat.Id == DefinedConstants.Zone.CacGiaiKhac ||
                    objCat.Id == DefinedConstants.Zone.BongDaVietNam)
                    {
                        phMostView.Controls.Add(LoadControl("/Controls/Sport/BoxBangXepHang.ascx"));
                    }
                    else
                    {
                        phMostView.Controls.Add(LoadControl("/Controls/Sport/MostViewNews.ascx"));

                    }
                }
                #endregion

                #region Title/MetaData
                var mtaCategory = bo.GetCategoryMetaData(objCat.Id);
                var extraText = containsPageIndex ? string.Format(" | page {0}", pageIndex) : string.Empty;
                if (mtaCategory != null)
                {
                    PresentationUtils.AddMeta(Page, DefinedConstants.MetaAttributes.Description.Id,
                                              DefinedConstants.MetaAttributes.Description.Name, mtaCategory.Description + extraText);
                    PresentationUtils.AddMeta(Page, DefinedConstants.MetaAttributes.Keywords.Id,
                                              DefinedConstants.MetaAttributes.Keywords.Name, mtaCategory.Keywords);
                    PresentationUtils.AddMeta(Page, DefinedConstants.MetaAttributes.NewsKeywords.Id,
                                              DefinedConstants.MetaAttributes.NewsKeywords.Name, mtaCategory.Keywords);
                    Page.Header.Title = string.Format("{0} | soha.vn {1}", mtaCategory.Title, extraText);
                }
                #endregion
                return;
            }
            _301Redirection.RedirectToFileNotFoundPage();
        }
    }
}